package com.asset.dispoal.business.reportAnalysis.service.common;

import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.global.CreateThreadPoolUtil;
import com.asset.dispoal.business.reportAnalysis.dto.HomeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.date.DateTimeUtil;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

@Slf4j
@Component
public class HomeStatisticsCommand {

    @Autowired
    private AgreementMapper agreementMapper;

    public HomeResponse execute() {
        HomeResponse homeResponse = new HomeResponse();
        LocalDate now = LocalDate.now();
        LocalDate weekStart = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate weekEnd = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
        LocalDate monthStart = now.withDayOfMonth(1);
        LocalDate monthEnd = now.with(TemporalAdjusters.lastDayOfMonth());
        LocalDate yearStart = LocalDate.of(now.getYear(), 1, 1);
        LocalDate yearEnd = LocalDate.of(now.getYear(), 12, 31);

        Date currentStartDay = DateTimeUtil.localDateToStartDate(now);
        Date currentEndDay = DateTimeUtil.localDateToEndDate(now);
        Date currentStartWeek = DateTimeUtil.localDateToStartDate(weekStart);
        Date currentEndWeek = DateTimeUtil.localDateToStartDate(weekEnd);
        Date currentStartMonth = DateTimeUtil.localDateToStartDate(monthStart);
        Date currentEndMonth = DateTimeUtil.localDateToStartDate(monthEnd);
        Date currentStartYear = DateTimeUtil.localDateToStartDate(yearStart);
        Date currentEndYear = DateTimeUtil.localDateToStartDate(yearEnd);

        ExecutorService homeExecutorService = CreateThreadPoolUtil.homeExecutorService;
        CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
            homeResponse.setContractNumber(agreementMapper.queryContractNum(currentStartDay,
                    currentEndDay, currentStartWeek, currentEndWeek, currentStartMonth, currentEndMonth,
                    currentStartYear, currentEndYear));
        }, homeExecutorService);
        CompletableFuture<Void> c2 = CompletableFuture.runAsync(() -> {
            homeResponse.setCaseNumber(agreementMapper.queryCaseNum(currentStartDay,
                    currentEndDay, currentStartWeek, currentEndWeek, currentStartMonth, currentEndMonth,
                    currentStartYear, currentEndYear));
        }, homeExecutorService);
        CompletableFuture<Void> c3 = CompletableFuture.runAsync(() -> {
            homeResponse.setLawyerNumber(agreementMapper.queryLawyerNum(currentStartDay,
                    currentEndDay, currentStartWeek, currentEndWeek, currentStartMonth, currentEndMonth,
                    currentStartYear, currentEndYear));
        }, homeExecutorService);
        CompletableFuture<Void> c4 = CompletableFuture.runAsync(() -> {
            homeResponse.setEvidenceNumber(agreementMapper.queryEvidenceNum(currentStartDay,
                    currentEndDay, currentStartWeek, currentEndWeek, currentStartMonth, currentEndMonth,
                    currentStartYear, currentEndYear));
        }, homeExecutorService);
        CompletableFuture<Void> c5 = CompletableFuture.runAsync(() -> {
            homeResponse.setContractMonth(agreementMapper.queryContractMonth(currentStartYear));
        }, homeExecutorService);
        CompletableFuture<Void> c6 = CompletableFuture.runAsync(() -> {
            homeResponse.setCaseMonth(agreementMapper.queryCaseMonth(currentStartYear));
        }, homeExecutorService);
        CompletableFuture<Void> c7 = CompletableFuture.runAsync(() -> {
            homeResponse.setLawyerMonth(agreementMapper.queryLawyerMonth(currentStartYear));
        }, homeExecutorService);
        CompletableFuture<Void> c8 = CompletableFuture.runAsync(() -> {
            homeResponse.setEvidenceMonth(agreementMapper.queryEvidenceMonth(currentStartYear));
        }, homeExecutorService);

        CompletableFuture.allOf(
                c1, c2, c3, c4, c5, c6, c7, c8
        ).join();
        return homeResponse;
    }
}
